package com.graduation.vaccine.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.vaccine.mapper.VaccineCatalogMapper;
import com.graduation.vaccine.pojo.VaccineCatalog;
import com.graduation.vaccine.service.VaccineCatalogService;
import com.graduation.vaccine.vo.AddVaccineCatalogVo;
import com.graduation.vaccine.vo.RespBean;
import com.graduation.vaccine.vo.RespBeanEnum;
import com.graduation.vaccine.vo.UpdateCatalogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhaoJunYao
 * @since 2022-04-20
 */
@Service
public class VaccineCatalogServiceImpl extends ServiceImpl<VaccineCatalogMapper, VaccineCatalog> implements VaccineCatalogService {

    @Autowired
    private VaccineCatalogMapper vaccineCatalogMapper;
    @Override
    public RespBean getAll(String type) {
        if(type.equals("all"))
            return RespBean.success(vaccineCatalogMapper.getAll());
        else
            return RespBean.success(vaccineCatalogMapper.getMoreAll());
    }

    @Override
    public RespBean sysGetList() {
        return RespBean.success(vaccineCatalogMapper.getMoreAll());
    }

    @Override
    public RespBean addOne(AddVaccineCatalogVo addVaccineCatalogVo) {
        //校验是否有相同名称
        VaccineCatalog vaccineCatalog=vaccineCatalogMapper.getCatalogByName(addVaccineCatalogVo.getName());
        if(null!=vaccineCatalog){
            return RespBean.error(RespBeanEnum.DUPLICATE_NAME,"名称重复");
        }
        int row=vaccineCatalogMapper.addOne(addVaccineCatalogVo);
        if(row<1){
            return RespBean.error(RespBeanEnum.INSERT_ERROR,"添加失败，请检查数据输入是否规范");
        }
        return RespBean.success();
    }

    @Override
    public RespBean updateCatalog(UpdateCatalogVo updateCatalogVo) {
        VaccineCatalog vaccineCatalog=vaccineCatalogMapper.getCatalogByName(updateCatalogVo.getName());
        if(null!=vaccineCatalog){
            return RespBean.error(RespBeanEnum.DUPLICATE_NAME,"名称重复");
        }

        int row=vaccineCatalogMapper.updateCatalog(updateCatalogVo);
        if(row<1){
            return RespBean.error(RespBeanEnum.INSERT_ERROR,"添加失败，请检查数据输入是否规范");
        }
        return RespBean.success();
    }

    @Override
    public RespBean getDropDownBox() {
        return RespBean.success(vaccineCatalogMapper.getDropDownBox());
    }
}
